#include "cbgdialog.h"

#include <buola/image/cimage.h>
#include <buola/io/cfolder.h>
#include <buola/gui/cmenu.h>
#include <buola/widgets/ctoolbar.h>
#include <buola/gui/cpropertyfield.h>
#include <buola/widgets/ccheckbox.h>
#include <buola/widgets/cnumbox.h>
#include <buola/widgets/cbutton.h>
#include <buola/fsgui/cfiledialog.h>
#include <buola/fsgui/cdirdialog.h>
#include <buola/widgets/cdialoglayoutmanager.h>
#include <buola/widgets/ctheme.h>

#define GROUP_LOWER     1
#define GROUP_BUTTONS   2
#define GROUP_UPPER     3

#define ID_FIELD_URL    1
#define ID_FIELD_TYPE   2

namespace buola { namespace bdesktop {

CBgDialog::CBgDialog(CDesktop *pDesktop)
{
    SetAutoDelete(true);
    mDesktop=pDesktop;
}

CBgDialog::~CBgDialog()
{
    mDesktop->mBgDialog=nullptr;
}

void CBgDialog::FillList()
{
    for(int i=0;i<mDesktop->mWallpapers.size();i++)
        AddFile(mDesktop->mWallpapers[i]->mURL);
}

void CBgDialog::UpdateDesktop()
{
    mDesktop->mWallpapers.clear();

    for(int i=0;i<mPictBox->GetItemCount();i++)
    {
        SWallpaper *lWallpaper=new SWallpaper;
        lWallpaper->mURL=mPictBox->GetItems()[i]->Value(ID_FIELD_URL).Get<CURL>();
        mDesktop->mWallpapers.emplace_back(lWallpaper);
    }

    mDesktop->SetNextBack();
    mDesktop->Refresh();
}

void CBgDialog::AddFile(const CURL &pURL)
{
    PImage lImage=CImage::GetURL(pURL);
    if(!lImage) return;
    lImage->Scale(CIntSize(100,75));

    gui::CListBox::SItem *lItem=mPictBox->NewItem(decode(pURL.GetName()));
    lItem->SetImage(lImage);
    lItem->Value(ID_FIELD_URL).Set(pURL);
    mPictBox->AddItem(lItem);
}

void CBgDialog::AddFolder(const CURL &pURL)
{
    PFolder lFolder=CFolder::Get(pURL);
    std::vector<PFile> lFiles;
    lFolder->GetAll(lFiles);

    for(int i=0;i<lFiles.size();i++)
    {
        if(lFiles[i]->IsFolder()) continue;

        if(lFiles[i]->GetURL().GetExtension()=="jpg")
        {
            AddFile(lFiles[i]->GetURL());
        }
    }
}

void CBgDialog::OnCreate()
{
    mResult=gui::DIALOG_CANCEL;

    Resize(CSize(500,360));
    SetTitle(WI18N("Desktop background settings"));

    gui::PMenu lMenu=new gui::CMenu;

    lMenu->Add(gui::new_menu_item(WI18N("add image"),"stock_insert_image"))->eTriggered.Connect(&CBgDialog::OnAddImage,this);
    lMenu->Add(gui::new_menu_item(WI18N("add folder"),"stock_open"))->eTriggered.Connect(&CBgDialog::OnAddFolder,this);

    mToolBar=new gui::CToolBar;
    mToolBar->CreateAndSet(this,lMenu);

    mPictBox=new gui::CListBox;
    mPictBox->Create(this);
    mPictBox->AddField(new gui::CPropertyField(L"url",L"url",L"url",nullptr,gui::CPropertyField::PLF_HIDDEN),ID_FIELD_URL);
    mPictBox->AddField(new gui::CPropertyField(L"int",L"mode",L"mode",nullptr,gui::CPropertyField::PLF_HIDDEN),ID_FIELD_TYPE);

    mChangeBox=new gui::CCheckBox;
    mChangeBox->Create(this);
    mChangeBox->SetCaption(WI18N("Change every "));

    mTimeBox=new gui::CNumBox;
    mTimeBox->Create(this);

    mOK=new gui::CButton;
    mOK->ePressed.Connect(&CBgDialog::OnOK,this);
    mOK->Create(this);
    mOK->SetCaption(WI18N("OK"));
    mCancel=new gui::CButton;
    mCancel->ePressed.Connect(&CBgDialog::OnCancel,this);
    mCancel->Create(this);
    mCancel->SetCaption(WI18N("Cancel"));

    gui::CDialogLayoutManager *lLayout=gui::CDialogLayoutManager::Get(this);

    lLayout->AddHalf(GROUP_LOWER,WLAY_BOTTOM-80,WLAY_B);
    lLayout->AddHalf(GROUP_UPPER,WLAY_BOTTOM-80,WLAY_T);
    lLayout->AddGroup(GROUP_BUTTONS,WLAY_RIGHT-180,WLAY_BOTTOM-40,
                      WLAY_RIGHT-20,WLAY_BOTTOM-20,std::wstring(),WLAY_DEFAULT,GROUP_LOWER);

    lLayout->At(mPictBox,20,10,WLAY_RIGHT-20,WLAY_BOTTOM-10,GROUP_UPPER,
                WLAY_FRAME|WLAY_LBLTOP,WI18N("Collection"));

    lLayout->At(mChangeBox,20,10,140,30,GROUP_LOWER);
    lLayout->At(mTimeBox,150,10,220,30,GROUP_LOWER,WLAY_FRAME);

    lLayout->ArrayGroup(WLAY_HORIZ,10*WLAY_PERC,GROUP_BUTTONS,2,
                                mOK,mCancel);
    FillList();

    mToolBar->Show();
    mPictBox->Show();
    mChangeBox->Show();
    mTimeBox->Show();
    mOK->Show();
    mCancel->Show();
}

void CBgDialog::OnOK()
{
    mResult=gui::DIALOG_OK;

    UpdateDesktop();
    Destroy();
}

void CBgDialog::OnCancel()
{
    mResult=gui::DIALOG_CANCEL;

    Destroy();
}

void CBgDialog::OnAddImage()
{
    CFileDialog lDialog;

    lDialog.Create(nullptr);
    lDialog.StayOnTopOf(this);
    lDialog.SetCurDir("/usr/share/wallpapers");
    lDialog.DoModal();

    if(lDialog.mResult==gui::DIALOG_OK)
        AddFile(lDialog.mFile);
}

void CBgDialog::OnAddFolder()
{
    CDirDialog lDialog;

    lDialog.Create(nullptr);
    lDialog.StayOnTopOf(this);
    lDialog.SetCurDir("/usr/share/wallpapers",true);
    lDialog.DoModal();

    if(lDialog.mResult==gui::DIALOG_OK)
        AddFolder(lDialog.mDir);
}

/*namespace bdesktop*/ } /*namespace buola*/ }
